package com.sgq.vue.utils.resource;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @author: 83778
 * @date: 2024-01-09 21:39
 * @desc:
 */
public class JsonUtil {

    /**
     *  从json中获取键的值
     * @param json
     * @param key
     * @return
     */
    public static Object value = null ;
    public static List<Object> valueList = new ArrayList();
    public static List<Object> getValue(String json, String key, Integer days){
//        System.out.println("json = " + json);
        if (json.startsWith("{")){
            JSONObject jsonObject = JSONObject.parseObject(json);
            if (jsonObject.containsKey(key)){
                value = jsonObject.get(key);
                valueList.add(value);
            }else {
                Iterator<Map.Entry<String, Object>> iterator = jsonObject.entrySet().iterator();
                while (iterator.hasNext()){
                    Map.Entry<String, Object> next = iterator.next();
                    String k = next.getKey();
                    Object v = next.getValue();
                    if (v.toString().startsWith("{")){
                        getValue(v.toString(),key,days);
                    }else if (v.toString().startsWith("[") ){
                        JSONArray arr = JSONObject.parseArray(v.toString());
                        for (int i = 0 ; i < arr.size() ; i++) {
                            getValue(arr.get(i).toString(),key,days);
                            if (valueList.size() == days){
                                   break;
                            }
                        }
                    }
                }

            }

        }
        return valueList;
    }

    public static void main(String[] args) {
        String a = "{\"code\":\"200\",\"updateTime\":\"2024-01-09T22:39+08:00\",\"fxLink\":\"https://www.qweather.com/weather/wuhan-101200101.html\",\"now\":{\"obsTime\":\"2024-01-09T22:32+08:00\",\"temp\":\"7\",\"feelsLike\":\"4\",\"icon\":\"502\",\"text\":\"霾\",\"wind360\":\"47\",\"windDir\":\"东北风\",\"windScale\":\"2\",\"windSpeed\":\"7\",\"humidity\":\"70\",\"precip\":\"0.0\",\"pressure\":\"1023\",\"vis\":\"4\",\"cloud\":\"10\",\"dew\":\"2\"},\"refer\":{\"sources\":[\"QWeather\"],\"license\":[\"CC BY-SA 4.0\"]}}";
        String b = "{\"status\":\"1\",\"count\":\"1\",\"info\":\"OK\",\"infocode\":\"10000\",\"lives\":[{\"province\":\"湖北\",\"city\":\"武汉市\",\"adcode\":\"420100\",\"weather\":\"晴\",\"temperature\":\"7\",\"winddirection\":\"东北\",\"windpower\":\"≤3\",\"humidity\":\"65\",\"reporttime\":\"2024-01-09 23:32:01\",\"temperature_float\":\"7.0\",\"humidity_float\":\"65.0\"}]}";
        String c = "[{\"province\":\"湖北\",\"city\":\"武汉市\",\"adcode\":\"420100\",\"weather\":\"晴\",\"temperature\":\"7\",\"winddirection\":\"东北\",\"windpower\":\"≤3\",\"humidity\":\"65\",\"reporttime\":\"2024-01-09 23:32:01\",\"temperature_float\":\"7.0\",\"humidity_float\":\"65.0\"}]";


    }
}
